<?php

namespace App\Api\Repositories\Log;

use App\Models\Log\LogUserBrowse as Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

/**
 * 浏览记录
 * 新的浏览记录直接添加，就的浏览记录不动。
 * 查询前20条记录时，通过分组、时间排序的方式获取前20条不重复的浏览记录。
 * 这样，后台可以看到真实的浏览记录；被浏览次数的统计数据缓存失效时，也可以从数据库中获取正确的统计数据。
 */
class LogUserBrowseRepositories
{
    protected $eloquentClass = Model::class;

    /**
     * 获取浏览记录是否存在
     * 判断是否已读
     *
     * @param string $id
     * @param int $uid
     * @return void
     */
    public function get_is_read($id, $uid)
    {
        $is_read = Redis::get("sir:{$uid}:{$id}");
        if ($is_read === NULL) {
            $res = $this->eloquentClass::where('result_id', $id)->where('uid', $uid)->first() ? '1' : '0';
            Redis::set("sir:{$uid}:{$id}", $res);
            $is_read = Redis::get("sir:{$uid}:{$id}");
        }
        return boolval($is_read);
    }

    /**
     * 添加浏览记录
     *
     * @param string $id
     * @param int $uid
     * @return void
     */
    public function set_is_read($id, $uid)
    {
        $res = $this->eloquentClass::create([
            'uid' => $uid,
            'result_id' => $id,
            'created_at' => date("Y-m-d H:i:s", time()),
            'updated_at' => date("Y-m-d H:i:s", time())
        ]);
        Redis::set("sir:{$uid}:{$id}", "1");
        $this->add_user_be_browse_count(substr($id, 1, 100));
    }

    /**
     * 获取会员被浏览的次数
     *
     * @param int $uid
     * @return void
     */
    public function get_user_be_browse_count($uid)
    {
        $count = Redis::get("bbc:{$uid}");
        if ($count === null) {
            $res = $this->eloquentClass::where('result_id', 'i' . $uid)->count();
            Redis::set("bbc:{$uid}", $res);
            $count = Redis::get("bbc:{$uid}");
        }
        return $count;
    }

    /**
     * 添加会员被浏览次数
     *
     * @param int $uid
     * @return void
     */
    public function add_user_be_browse_count($uid)
    {
        $this->get_user_be_browse_count($uid);
        Redis::incr("bbc:{$uid}");
        return true;
    }

    /**
     * 获取浏览历史及其相关数据
     *
     * @param int $uid
     * @return void
     */
    public function use_uid_get_log($uid)
    {
        $result_ids = $this->eloquentClass::where('uid', $uid)->orderBy('id', 'desc')->distinct()->limit(20)->pluck('result_id')->toArray();
        $log = [];
        foreach ($result_ids as $result_id) {
            $log[] = $this->eloquentClass::select('id', 'uid', 'result_id', 'updated_at')->where('uid', $uid)->where('result_id', $result_id)->orderBy('id', 'desc')->first();
        }
        // $log = $this->eloquentClass::select('id', 'uid', 'result_id', 'updated_at')->where('uid', $uid)->groupBy('result_id')->orderBy('id', 'desc')->limit(20)->get();
        foreach ($log as $key => &$v) {
            if (substr($v->result_id, 0, 1) == 'i') {
                $udr = new \App\Api\Repositories\User\UserDetailRepositories();
                $detail = $udr->use_id_get_data(substr($v->result_id, 1, 100));
                if ($detail) {
                    $v->detail = $detail;
                } else {
                    unset($log[$key]);
                }
            } else {
                $sr = new \App\Api\Repositories\Search\SearchExogenousDataRepositories();
                $v->detail = $sr->use_id_get_content(substr($v->result_id, 1, 100));
            }
        }
        return $log;
    }
}
